import com.huawei.codecheck.uccp.api.base.PluginExtensionPoint;
import com.huawei.codecheck.uccp.api.bean.param.RuleInfo;
import com.huawei.codecheck.uccp.api.bean.param.ToolParameterInfo;
import com.huawei.codecheck.uccp.api.bean.param.ToolRuleSetInfo;

import com.google.gson.Gson;
import org.apache.commons.io.FileUtils;
import org.pf4j.PluginManager;
import org.pf4j.PluginWrapper;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;

/**
 * 自测试类
 */
public class ThirdPartyToolsPluginTest {


    /**
     * 本地调试类，平台也是通过这种方式调起三方引擎插件的
     * 1、先执行 ：mvn clean package -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -f ./pom.xml -s ./ci/settings.xml
     * 2、获取上面的命令在工程的target目录生成插件jar包名字
     * 3、src/test/resources/toolParameterInfo.json 修改入参文件
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        // 4  填写target生成的jar包名
        String pluginName = "uccp-CjLintPlugin-1.0.0.jar";
//        PluginExtensionPoint plugin = getPlugin(pluginName);

        ToolParameterInfo toolParameterInfo = getToolParameterInfo(false);

        // 5 默认报告输出路径放到target/test-class下
        String reportPath = ThirdPartyToolsPluginTest.class.getClassLoader().getResource(".").toURI().getPath();
        toolParameterInfo.setReportDirPath(reportPath);
        boolean bo = toolParameterInfo.isFullCheck() || toolParameterInfo.getSourceFiles() != null && toolParameterInfo.getSourceFiles().size() > 0;
//        plugin.execute(toolParameterInfo);
        System.out.println();
    }

    public static void createToolParameterInfo() throws IOException {
        ToolParameterInfo toolParameterInfo = new ToolParameterInfo();
        toolParameterInfo.setProjectRoot("/devcloud/workspace/code");
        toolParameterInfo.setExclude("test|demo");
        toolParameterInfo.setIncludePaths(new ArrayList<>());
        toolParameterInfo.getIncludePaths().add("src1");
        toolParameterInfo.getIncludePaths().add("src2");
        toolParameterInfo.setIncludePathsStr("src1,src2");
        toolParameterInfo.setMode(0);
        toolParameterInfo.setSourceFiles(new ArrayList<>());
        toolParameterInfo.getSourceFiles().add("/devcloud/workspace/code/src1/test1.java");
        toolParameterInfo.getSourceFiles().add("/devcloud/workspace/code/src2/test2.java");
        toolParameterInfo.getSourceFiles().add("/devcloud/workspace/code/src3/test13.java");
        toolParameterInfo.setRuleSets(new ArrayList<>());
        ToolRuleSetInfo rule = new ToolRuleSetInfo();
        rule.setLanguage("java");
        rule.setRules(new ArrayList<>());
        RuleInfo ruleInfo = new RuleInfo();
        ruleInfo.setRuleName("ruleName1");
        ruleInfo.setRuleType("ruleType1");
        ruleInfo.setCustomFileId("xxxx");
        rule.getRules().add(ruleInfo);
        toolParameterInfo.getRuleSets().add(rule);
        toolParameterInfo.setReportDirPath("/devcloud/workspace/report/thirdparty");
        String json = new Gson().toJson(toolParameterInfo);
        String paramInfoPath = ThirdPartyToolsPluginTest.class.getClassLoader().getResource("toolParameterInfo.json").getPath();
        FileUtils.write(new File(paramInfoPath), json, "utf-8");
    }

    public static ToolParameterInfo getToolParameterInfo(boolean resetProject) throws IOException {
        if (resetProject) {
            createToolParameterInfo();
        }
        String paramInfoPath = ThirdPartyToolsPluginTest.class.getClassLoader().getResource("toolParameterInfo.json").getPath();
        String context = FileUtils.readFileToString(new File(paramInfoPath), "utf-8");
        return new Gson().fromJson(context, ToolParameterInfo.class);
    }


    /**
     * 不要动这个方法，入参是生成的插件jar包名字
     *
     * @param pluginJarName 插件jar包名字
     * @return  插件
     * @throws Exception 异常
     */
    private static PluginExtensionPoint getPlugin(String pluginJarName) throws Exception {
        PluginManager pluginManager = new JarPluginManager(ThirdPartyToolsPluginTest.class.getClassLoader());

        String root = Paths.get(ThirdPartyToolsPluginTest.class.getClassLoader().getResource(".").toURI()).toString();

        String pluginPath = root + File.separator + "../" + pluginJarName;
        pluginManager.loadPlugin(new File(pluginPath).toPath());

        for (PluginWrapper pluginWrapper : pluginManager.getPlugins()) {
            pluginManager.startPlugin(pluginWrapper.getPluginId());
        }
        List<PluginExtensionPoint> pluginExtensionPoint = pluginManager.getExtensions(PluginExtensionPoint.class);

        for (PluginWrapper pluginWrapper : pluginManager.getPlugins()) {
            pluginManager.startPlugin(pluginWrapper.getPluginId());
        }
        PluginExtensionPoint plugin = pluginExtensionPoint.get(1);
        // 当任务执行完成, 停止插件执行
        Runtime.getRuntime().addShutdownHook(new Thread(pluginManager::stopPlugins));
        return plugin;
    }



}
